package com.eh.observer.subject.impl;

import com.eh.observer.dto.Event;
import com.eh.observer.observer.Observer;
import com.eh.observer.subject.Subject;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * Created by David on 2016/4/23.
 */
public class WeatherData implements Subject {
    private List<Observer> observers;
    private float temperature;
    private float humidity;

    public WeatherData() {
        observers = Lists.newArrayList();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyAllObservers() {
        for (Observer observer : observers) {
            observer.update(new Event(temperature, humidity));
        }
    }

    public void measurementsChanged() {
        notifyAllObservers();
    }

    public void setMeasurements(float temperature, float humidity) {
        this.temperature = temperature;
        this.humidity = humidity;
        measurementsChanged();
    }
}
